package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;

import java.util.List;

public class CreateGWForm extends ExecuteListener implements ExecuteListenerInterface {
    @Override
    public void execute(ProcessExecutionContext ctx) throws Exception {
        BO mainBo = ctx.getBO("BO_EU_SAP_WLZSJ");
        String isbg = mainBo.getString("IS_BG");
        if ("1".equals(isbg)){
            //币种
            String currency = mainBo.getString("CURRENCY");
            String processInstId = mainBo.getBindId();
            List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_JBST").addQuery("BINDID=", processInstId).list();
            BO boGird = list.get(0);
            String matlGroup = boGird.getString("MATL_GROUP");
            String type = boGird.getString("MATL_TYPE");
            if (currency.equals("USD")) {
                boolean isTs= false;
                if ("Z001/Z005".contains(type)){
                    isTs = true;
                }
                if ("Z002".equals(type)){
                    if ("1-WF-00/1-SB-00/1-IC-00".contains(matlGroup)){
                        isTs = true;
                    }
                }
                if (isTs){

                    BO newBo = SDK.getBOAPI().query("BO_EU_MM_GW").bindId(mainBo.getBindId()).detail();
                    if (newBo != null) {
                        newBo.set("MATL_TYPE", boGird.getString("MATL_TYPE"));
                        newBo.set("KH", boGird.getString("PAGEFORMAT"));
                        newBo.set("GYS", boGird.getString("PAGEFORMAT"));
                        newBo.set("XH", boGird.getString("BASIC_MATL"));
                        newBo.set("BZ", currency);
                        newBo.set("PLANT", boGird.getString("PLANT"));
                        newBo.set("MATERIAL", boGird.getString("MATERIAL"));
                        newBo.set("MATL_DESC", boGird.getString("MATL_DESC"));
                        newBo.set("TEXT_LINE", boGird.getString("TEXT_LINE"));
                        newBo.set("BASE_UOM", boGird.getString("BASE_UOM"));
                        newBo.set("BASIC_MATL", boGird.getString("BASIC_MATL"));
                        newBo.set("PAGEFORMAT", boGird.getString("PAGEFORMAT"));
                        newBo.set("MATL_GROUP", boGird.getString("MATL_GROUP"));
                        SDK.getBOAPI().update("BO_EU_MM_GW", newBo);
                    } else {
                        newBo = new BO();
                        newBo.set("KH", boGird.getString("PAGEFORMAT"));
                        newBo.set("GYS", boGird.getString("PAGEFORMAT"));
                        newBo.set("XH", boGird.getString("BASIC_MATL"));
                        newBo.set("BZ", currency);
                        String at1 = "";
                        if (matlGroup.contains("1-WF")){
                            at1 = "LJW" + boGird.getString("PAGEFORMAT") + "@sequence(LJW" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        if (matlGroup.contains("1-IC")){
                            at1 = "LJP" + boGird.getString("PAGEFORMAT") + "@sequence(LJP" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        if (matlGroup.contains("1-SB")){
                            at1 = "LJS" + boGird.getString("PAGEFORMAT") + "@sequence(LJS" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        newBo.set("LJ_YXDZ", SDK.getRuleAPI().executeAtScript(at1));
                        String at2 = "";
                        if (matlGroup.contains("1-WF")){
                            at2 = "BDTW" + boGird.getString("PAGEFORMAT") + "@sequence(BDTW" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        if (matlGroup.contains("1-IC")){
                            at2 = "BDTP" + boGird.getString("PAGEFORMAT") + "@sequence(BDTP" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        if (matlGroup.contains("1-SB")){
                            at2 = "BDTS" + boGird.getString("PAGEFORMAT") + "@sequence(BDTS" + boGird.getString("PAGEFORMAT") + ",4,0)";
                        }
                        newBo.set("LJ_BDT", SDK.getRuleAPI().executeAtScript(at2));
                        newBo.set("MATL_TYPE", boGird.getString("MATL_TYPE"));
                        newBo.set("PLANT", boGird.getString("PLANT"));
                        newBo.set("MATERIAL", boGird.getString("MATERIAL"));
                        newBo.set("MATL_DESC", boGird.getString("MATL_DESC"));
                        newBo.set("TEXT_LINE", boGird.getString("TEXT_LINE"));
                        newBo.set("BASE_UOM", boGird.getString("BASE_UOM"));
                        newBo.set("BASIC_MATL", boGird.getString("BASIC_MATL"));
                        newBo.set("PAGEFORMAT", boGird.getString("PAGEFORMAT"));
                        newBo.set("MATL_GROUP", boGird.getString("MATL_GROUP"));
                        SDK.getBOAPI().create("BO_EU_MM_GW", newBo, mainBo.getBindId(), ctx.getUserContext().getUID());
                    }
                }
            }
        }
    }
}
